﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ANOWORD=Microsoft.Office.Interop.Word;

namespace HJ_SQL_ToTable
{
    class ReportSQLWriter : IWriteDocument
    {
        public bool WriteSumary(ANOWORD.Document docu, List<SQLTable> tables)
        {
            bool bRet = false;
            int nRowCount;
            ANOWORD.Paragraph para;
            ANOWORD.Table tabDocu;
            string strTableName = string.Empty;
            ANOWORD.Range tabRange;
            try
            {
                strTableName = "数据库表";
                para = docu.Paragraphs.Add();
                para.Range.Font.Name = "宋体";
                para.Range.Font.Size = float.Parse("12");
                para.Range.Text = strTableName;
                para.Range.Font.Bold = 1;
                object style_Heading1 = "标题 3";
                para.Range.ParagraphFormat.set_Style(ref style_Heading1);

                para = docu.Paragraphs.Add();
                nRowCount = tables.Count + 2;
                tabRange = docu.Range(docu.Content.End - 1, docu.Content.End);
                tabDocu = docu.Tables.Add(tabRange, nRowCount, 3);

                tabDocu.Range.Font.Name = "宋体";
                tabDocu.Range.Font.Size = float.Parse("12");
                tabDocu.Borders.InsideLineStyle = ANOWORD.WdLineStyle.wdLineStyleSingle;
                tabDocu.Borders.InsideColorIndex = ANOWORD.WdColorIndex.wdAuto;
                tabDocu.Borders.OutsideLineStyle = ANOWORD.WdLineStyle.wdLineStyleSingle;
                tabDocu.Borders.OutsideColorIndex = ANOWORD.WdColorIndex.wdAuto;

                tabDocu.Cell(1, 1).Range.Text = "序号";
                tabDocu.Cell(1, 2).Range.Text = "表名";
                tabDocu.Cell(1, 3).Range.Text = "功能说明";
                //tabDocu.Cell(1, 4).Range.Text = "说明";
                SetAlignCenter(tabDocu.Cell(1, 1).Range);
                SetAlignCenter(tabDocu.Cell(1, 2).Range);
                SetAlignCenter(tabDocu.Cell(1, 3).Range);

                //SetAlignCenter(tabDocu.Cell(1, 4).Range);
                //InitTable(tabDocu, strTableName, nRowCount, 4);
                OutputSumaryTable(tabDocu, tables, nRowCount, 2);

                docu.Paragraphs.Add();

                bRet = true;
            }
            catch (Exception ex)
            {

            }

            return bRet;
        }

        public bool OutputSumaryTable(ANOWORD.Table table, List<SQLTable> tables, int nRowCount, int nIndex)
        {
            int nCurIndex = 0;
            foreach(var tableSql in tables)
            {
                table.Cell(nIndex + nCurIndex, 1).Range.Text = (nCurIndex + 1).ToString();
                table.Cell(nIndex + nCurIndex++, 2).Range.Text = tableSql.TableName.Replace("edx", "onlineCourse"); ;
            }
            return true;
        }
        public bool WriteDocument(ANOWORD.Document docu, SQLTable table)
        {
            bool bRet = false;
            int nRowCount;
            ANOWORD.Paragraph para;
            ANOWORD.Table tabDocu;
            string strTableName = string.Empty;
            ANOWORD.Range tabRange;
            try
            {
                strTableName = table.TableName.Replace("edx", "onlineCourse");
                para = docu.Paragraphs.Add();
                para.Range.Font.Name = "宋体";
                para.Range.Font.Size = float.Parse("12");
                para.Range.Text = strTableName;
                para.Range.Font.Bold = 1;
                //object style_Heading1 = "标题 4";
                //para.Range.ParagraphFormat.set_Style(ref style_Heading1);

                para = docu.Paragraphs.Add();
                nRowCount = 3 + table.Fields.Count + 2;
                tabRange = docu.Range(docu.Content.End - 1, docu.Content.End);
                tabDocu = docu.Tables.Add(tabRange, nRowCount, 4);
                
                tabDocu.Range.Font.Name = "宋体";
                tabDocu.Range.Font.Size = float.Parse("12");
                tabDocu.Range.Font.Bold = 0;
                tabDocu.Borders.InsideLineStyle = ANOWORD.WdLineStyle.wdLineStyleSingle;
                tabDocu.Borders.InsideColorIndex = ANOWORD.WdColorIndex.wdAuto;
                tabDocu.Borders.OutsideLineStyle = ANOWORD.WdLineStyle.wdLineStyleSingle;
                tabDocu.Borders.OutsideColorIndex = ANOWORD.WdColorIndex.wdAuto;
                InitTable(tabDocu, strTableName, nRowCount, 4);
                OutputTable(tabDocu, table, nRowCount, 4, 3);
                
                docu.Paragraphs.Add();

                bRet = true;
            }
            catch (Exception ex)
            {

            }

            return bRet;
        }

        public bool InitTable(ANOWORD.Table table, string tableName, int nRowCount, int nColumnCount)
        {

            if (table.Rows.Count != nRowCount)
                return false;

            //tabDocu.Cell(2, 2).Merge(tabDocu.Cell(2, 3));
            try
            {
                table.Cell(1, 1).Range.Text = "表名/缩写";
                SetAlignCenter(table.Cell(1, 1).Range);

                MergeTable(table, 1, 2, 2);
                table.Cell(1, 2).Range.Text = tableName;
                SetAlignCenter(table.Cell(1, 2).Range);

                table.Cell(2, 1).Range.Text = "功能描述";
                SetAlignCenter(table.Cell(2, 1).Range);
                MergeTable(table, 2, 2, 2);
                SetAlignCenter(table.Cell(2, 2).Range);
                //table.Cell(1, 1).Range.Text = "功能描述";

                table.Cell(3, 1).Range.Text = "列名";
                table.Cell(3, 2).Range.Text = "数据类型";
                table.Cell(3, 3).Range.Text = "可否为空";
                table.Cell(3, 4).Range.Text = "说明";
                SetAlignCenter(table.Cell(3, 1).Range);
                SetAlignCenter(table.Cell(3, 2).Range);
                SetAlignCenter(table.Cell(3, 3).Range);
                SetAlignCenter(table.Cell(3, 4).Range);

                table.Cell(nRowCount, 1).Range.Text = "补充说明";
                SetAlignCenter(table.Cell(nRowCount, 1).Range);

                MergeTable(table, nRowCount , 2, 2);
                SetAlignCenter(table.Cell(nRowCount, 2).Range);
            }
            catch (Exception ex)
            {
                return false;
            }

            return true;
        }

        public void SetAlignCenter(ANOWORD.Range selRange)
        {
            if (selRange != null)
                selRange.ParagraphFormat.Alignment = ANOWORD.WdParagraphAlignment.wdAlignParagraphCenter;
        }

        public bool MergeTable(ANOWORD.Table table, int nRowIndex, int nColumnIndex, int nMergeCount)
        {
            ANOWORD.Cell cell;
            ANOWORD.Cell toMergeCell;
            bool bRet = false;
            int i;
            try
            {
                cell = table.Cell(nRowIndex, nColumnIndex);
                for (i = 0; i < nMergeCount; i++)
                {
                    toMergeCell = table.Cell(nRowIndex, nColumnIndex + 1);
                    toMergeCell.Merge(cell);
                }

                bRet = true;
            }
            catch (Exception ex) { }
            return bRet;
        }

        public bool OutputTable(ANOWORD.Table msTable, SQLTable sqlTable, int nRowCount, int nColumnCount, int nInitIndex)
        {
            //tabDocu.Cell(2, 2).Merge(tabDocu.Cell(2, 3));
            //table.Cell(2, 2).Merge(table.Cell(2, 3));
            //docu.Tables[docu.Tables.Count - 1].

            if (msTable == null || sqlTable == null)
                return false;

            int nIndex = nInitIndex;
            bool faild = false;
            
            foreach (var field in sqlTable.Fields)
            {
                if (!OutputRow(msTable, field, nRowCount, nColumnCount, ++nIndex))
                {
                    faild = true;
                    break;
                }
            }

            if (faild)
                return false;

            return true;
        }

        public bool OutputRow(ANOWORD.Table msTable, SQLField field, int nRowCount, int nColumnCount, int nInitIndex)
        {
            //tabDocu.Cell(2, 2).Merge(tabDocu.Cell(2, 3));
            //table.Cell(2, 2).Merge(table.Cell(2, 3));
            //docu.Tables[docu.Tables.Count - 1].

            if (msTable == null || field == null)
                return false;

            //if ( sqlTable.Fields.Count + nInitIndex + 1)
            ANOWORD.Cell cellEdit;
            bool bRet = false;

            try
            {
                cellEdit = msTable.Cell(nInitIndex, 1);
                cellEdit.Range.Text = field.FieldName.Replace("edx", "onlineCourse");

                cellEdit = msTable.Cell(nInitIndex, 2);
                cellEdit.Range.Text = field.FieldType;

                cellEdit = msTable.Cell(nInitIndex, 3);
                cellEdit.Range.Text = field.CanSetNull?"可":"否";

                bRet = true;
            }
            catch (Exception ex) { }

            return bRet;
        }

        public bool WriteDocument(ANOWORD.Document docu, List<SQLTable> tables)
        {
            if (docu == null || tables == null)
                return false;

            if (tables.Count == 0)
                return false;

            bool bRet = false;
            bool errorOccurs = false;

            try
            {
                if (!WriteSumary(docu, tables))
                    return false;

                foreach (var table in tables) {
                    if (!WriteDocument(docu, table))
                    {
                        errorOccurs = true;
                        break;
                    }
                }

                if ( !errorOccurs)
                    bRet = true;
            }
            catch (Exception ex)
            {

            }

            return bRet;
        }
    }
}
